﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using Intorise.Net;

namespace RemoteControlModel
{
    [Serializable]
    public class Message
    {
        public MessageTypeEnum MessageType { get; set; }

        /// <summary>
        /// 获取序列化的数据，前面为数据长度
        /// </summary>
        /// <returns></returns>
        public virtual byte[] Serialize()
        {
            MemoryStream stream = new MemoryStream();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            binaryFormatter.Serialize(stream, this);

            NetTools net = new NetTools();
            var data=net.GetNetworkBytes((int)stream.Length);
            MemoryStream streamNew = new MemoryStream();
            streamNew.Write(data, 0, data.Length);
            stream.WriteTo(streamNew);
            return streamNew.ToArray();
        }
    }
}
